iT邦幫忙

2024 iThome 鐵人賽

DAY 7
0
Odoo

30天就算 0 基礎,也能使用 GenAI 創造簡單的 Odoo 模組應用系列 第 7

【Day07】迴圈控制使用情境:購物車總金額、連鎖店調貨邏輯

  • 分享至 

  • xImage
  •  

https://ithelp.ithome.com.tw/upload/images/20240921/20163326BRwARlvknO.png
https://ithelp.ithome.com.tw/upload/images/20240921/20163326UWHanj7qaw.png
https://ithelp.ithome.com.tw/upload/images/20240921/20163326W05UA86OQN.png

計算購物車總金額 (for)

在電商平台上,當客戶加入多個商品到購物車時,需要計算總金額。這可以通過 for loop 來累加購物車中所有商品的價格。

# 假設客戶購物車中的商品
cart = {'Product A': 2, 'Product B': 1, 'Product C': 3}  # 產品和數量
prices = {'Product A': 100, 'Product B': 150, 'Product C': 200}

total_amount = 0

for product, quantity in cart.items():
    total_amount += prices[product] * quantity
    print(f"{product}: {quantity} x ${prices[product]} = ${prices[product] * quantity}")

print(f"Total cart amount: ${total_amount}")

從不同的連鎖店調貨 (while)

連鎖店的調貨情境,可以假設一個總部需要從不同的分店調貨。當某個分店的庫存不足時,總部會轉向其他分店直到調足訂單需求。

# 各連鎖店的庫存數據
stores = {
    'Store A': 30,
    'Store B': 50,
    'Store C': 20,
    'Store D': 40
}

# 訂單需求的數量
order_quantity = 100

# 初始化變數
total_sourced = 0  # 已調到的總數量
current_store_index = 0  # 目前正在查詢的店鋪索引
store_list = list(stores.keys())  # 將店鋪名單轉換成列表,便於索引遍歷

while total_sourced < order_quantity and current_store_index < len(store_list):
    current_store = store_list[current_store_index]
    available_stock = stores[current_store]

    if available_stock > 0:
        # 計算從該店鋪能調到的數量
        to_source = min(order_quantity - total_sourced, available_stock)
        total_sourced += to_source
        stores[current_store] -= to_source
        print(f"Fetched {to_source} units from {current_store}. Total sourced: {total_sourced} units.")
    else:
        print(f"{current_store} has no stock left.")

    # 移動到下一個店鋪
    current_store_index += 1

# 調貨結果
if total_sourced >= order_quantity:
    print(f"Order fulfilled! Total sourced: {total_sourced} units.")
else:
    print(f"Unable to fully source the order. Total sourced: {total_sourced} units, {order_quantity - total_sourced} units still needed.")

從分公司調貨,達到訂單需求時立即停止 (break)

如何在達到訂單需求時使用 break 立即結束調貨流程。

# 各分公司的庫存數據
branches = {
    'Branch A': 30,
    'Branch B': 50,
    'Branch C': 20,
    'Branch D': 40
}

# 訂單需求的數量
order_quantity = 70

# 初始化變數
total_sourced = 0  # 已調到的總數量
current_branch_index = 0  # 目前正在查詢的分公司索引
branch_list = list(branches.keys())  # 將分公司名單轉換成列表,便於索引遍歷

while current_branch_index < len(branch_list):
    current_branch = branch_list[current_branch_index]
    available_stock = branches[current_branch]

    if available_stock > 0:
        to_source = min(order_quantity - total_sourced, available_stock)
        total_sourced += to_source
        branches[current_branch] -= to_source
        print(f"Fetched {to_source} units from {current_branch}. Total sourced: {total_sourced} units.")

        # 如果已經達到訂單需求,結束調貨
        if total_sourced >= order_quantity:
            print("Order fulfilled!")
            break  # 立即結束迴圈
    else:
        print(f"{current_branch} has no stock left.")

    current_branch_index += 1

if total_sourced < order_quantity:
    print(f"Unable to fully source the order. Still need {order_quantity - total_sourced} units.")

忽略無庫存的分公司並繼續下一個 (continue)

使用 continue 當分公司庫存為 0 時跳過該公司,並繼續查找下一個分公司。

# 各分公司的庫存數據
branches = {
    'Branch A': 0,   # 無庫存
    'Branch B': 50,
    'Branch C': 0,   # 無庫存
    'Branch D': 40
}

# 訂單需求的數量
order_quantity = 80

# 初始化變數
total_sourced = 0  # 已調到的總數量
current_branch_index = 0  # 目前正在查詢的分公司索引
branch_list = list(branches.keys())  # 將分公司名單轉換成列表,便於索引遍歷

while current_branch_index < len(branch_list):
    current_branch = branch_list[current_branch_index]
    available_stock = branches[current_branch]

    # 如果該分公司無庫存,跳過該分公司
    if available_stock == 0:
        print(f"{current_branch} has no stock, moving to next branch.")
        current_branch_index += 1
        continue  # 跳過剩餘的處理,繼續下一個迴圈

    to_source = min(order_quantity - total_sourced, available_stock)
    total_sourced += to_source
    branches[current_branch] -= to_source
    print(f"Fetched {to_source} units from {current_branch}. Total sourced: {total_sourced} units.")

    if total_sourced >= order_quantity:
        print("Order fulfilled!")
        break

    current_branch_index += 1

if total_sourced < order_quantity:
    print(f"Unable to fully source the order. Still need {order_quantity - total_sourced} units.")

上一篇
【Day06】ERP 系統中的布林邏輯與條件語句應用:銷售折扣、庫存管理、用戶權限 ...
下一篇
【Day08】函數設計 (function):自動化報表輸出和客戶資料管理
系列文
30天就算 0 基礎,也能使用 GenAI 創造簡單的 Odoo 模組應用21
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言